﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Text;

namespace Tools
{
    class DebugLog
    {
        private static bool IsNeedLog()
        {
            bool bl = false;
            string open = ConfigurationManager.AppSettings["WriteDebugLog"];
            open = string.IsNullOrEmpty(open) ? "" : open.ToString();
            if (open == "1" || open == "2") bl = true;
            return bl;
        }

        /// <summary>接口调用记录</summary>
        public static void WriteLog(string mess)
        {
            try
            {
                if (IsNeedLog() == false) return;
                string path = AppDomain.CurrentDomain.BaseDirectory + "log\\" + DateTime.Now.ToString("yyyy-MM-dd");
                if (!Directory.Exists(path)) Directory.CreateDirectory(path);
                string filePath = path + @"\接口记录.txt";
                System.IO.FileStream fs = new System.IO.FileStream(filePath, System.IO.FileMode.Append);
                System.IO.StreamWriter sw = new System.IO.StreamWriter(fs);
                sw.WriteLine("[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "] " + mess);
                sw.Flush();
                sw.Close();
                fs.Close();
            }
            catch (Exception) { }
        }

        /// <summary>返回大数据的专用接口</summary>
        public static void WriteLog(int returnCount, string returnData)
        {
            string flag = ConfigurationManager.AppSettings["WriteDebugLog"];
            flag = string.IsNullOrEmpty(flag) ? "" : flag.ToString();
            if (flag == "2")
            {
                WriteLog("return data=" + returnData);
                return;
            }
            else if (flag == "1")
            {
                WriteLog("return count=" + returnCount);
                return;
            }
        }

        /// <summary>写报错日志</summary>
        public static void WriteErrLog(string mess)
        {
            try
            {
                string path = AppDomain.CurrentDomain.BaseDirectory + "log\\" + DateTime.Now.ToString("yyyy-MM-dd");
                if (!Directory.Exists(path)) Directory.CreateDirectory(path);
                string filePath = path + @"\运行错误.txt";
                System.IO.FileStream fs = new System.IO.FileStream(filePath, System.IO.FileMode.Append);
                System.IO.StreamWriter sw = new System.IO.StreamWriter(fs);
                sw.WriteLine("[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "] " + mess);
                sw.Flush();
                sw.Close();
                fs.Close();
            }
            catch (Exception) { }
        }

    }
}
